package com.hsyf.service.admin;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.jdiy.core.App;
import net.jdiy.core.Dao;
import net.jdiy.core.Ls;
import net.jdiy.core.Rs;


/**
 * 获取子节点
 */
public class NodeUtil {
    App app = App.get();//取得JDiy App上下文
    Dao dao = app.getDao(); //取得默认的数据库操作DAO对象(DataAccessObject)
	private List<Long> returnList = new ArrayList<Long>();
	
	/**
	 * 根据父节点的ID获取所有子节点
	 * @param list 分类表
	 * @param typeId 传入的父节点ID
	 * @return String
	 */
	public List getChildNodes() {
			Ls ls= dao.ls("SELECT t0,id,tid,t1 FROM front_menu WHERE tid='0000000000' and t1 is null", 0, 1);
			for(Rs rs:ls.getItems()){
				System.out.println(rs.getString("t0"));
			}


		return returnList;
	}
	
	private void recursionFn(List<Node> list, Node node) {
		List<Node> childList = getChildList(list, node);// 得到子节点列表
		if (hasChild(list, node)) {// 判断是否有子节点
			returnList.add(node.getId());
			Iterator<Node> it = childList.iterator();
			while (it.hasNext()) {
				Node n = (Node) it.next();
				recursionFn(list, n);
			}
		} else {
			returnList.add(node.getId());
		}
	}
	
	// 得到子节点列表
	private List<Node> getChildList(List<Node> list, Node node) {
		List<Node> nodeList = new ArrayList<Node>();
		Iterator<Node> it = list.iterator();
		while (it.hasNext()) {
			Node n = (Node) it.next();
			if (n.getParentId() == node.getId()) {
				nodeList.add(n);
			}
		}
		return nodeList;
	}

	// 判断是否有子节点
	private boolean hasChild(List<Node> list, Node node) {
		return getChildList(list, node).size() > 0 ? true : false;
	}
	
	
	// 本地模拟数据测试
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		List<Node> nodeList = new ArrayList<Node>();
		Node node1 = new Node(1l, "蔬菜", 0l);
		Node node2 = new Node(2l, "水产", 0l);
		Node node3 = new Node(3l, "畜牧", 0l);
		Node node4 = new Node(4l, "瓜类", 1l);
		Node node5 = new Node(5l, "叶类", 1l);
		Node node6 = new Node(6l, "丝瓜", 4l);
		Node node7 = new Node(7l, "黄瓜", 4l);
		Node node8 = new Node(8l, "白菜", 1l);
		Node node9 = new Node(9l, "虾", 2l);
		Node node10 = new Node(10l, "鱼", 2l);
		Node node11 = new Node(11l, "牛", 3l);
		
		nodeList.add(node1);
		nodeList.add(node2);
		nodeList.add(node3);
		nodeList.add(node4);
		nodeList.add(node5);
		nodeList.add(node6);
		nodeList.add(node7);
		nodeList.add(node8);
		nodeList.add(node9);
		nodeList.add(node10);
		nodeList.add(node11);
		
		NodeUtil mt = new NodeUtil();
		//System.out.println(mt.getChildNodes(nodeList, 1l));
		long end = System.currentTimeMillis();
		System.out.println("用时:" + (end - start) + "ms");
	}
	
}